//
//  LYComposeView.swift
//  微博
//
//  Created by liuerying on 16/5/28.
//  Copyright © 2016年 liuerying. All rights reserved.
//

import UIKit

import SnapKit

class LYComposeView: UIView {

    //按钮数组保存
    private lazy var buttons:[LYComposeButton] = [LYComposeButton]()

    //保存 控制器 参数,方便使用
    weak var target:UIViewController?
    
    func show(targetVC target:UIViewController){
    
        self.target = target
        target.view.addSubview(self)
    }
    
    override init(frame: CGRect) {
      
        //指定大小
        let rect = UIScreen.mainScreen().bounds
        super.init(frame: rect)
    
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //点击屏幕退出
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        //反转 遍历按钮数组  按顺序移除
        for (index,btn) in buttons.reverse().enumerate(){
        
            startAnimation(btn, index: index, offset: 350)
            
        }
    }
    
    
    //将要移动到父视图之后开始做动画
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(superview)
        
        //根据角标遍历数组buttons
        for (index,btn)in buttons.enumerate(){
        
            startAnimation(btn, index: index, offset: -350)
            
        }
        
    }
    
    //按钮的动画
    private func startAnimation(btn:LYComposeButton,index:Int,offset:CGFloat){
    
        UIView.animateWithDuration(0.8, delay:Double(index)*0.02, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: [], animations: {
            
            //修改按钮的Y值
            btn.center = CGPoint(x: btn.center.x, y: btn.center.y + offset)
            
            }) { (_) in
                //如果大于0 ,就移除
                if offset > 0 {
                
                    self.removeFromSuperview()
                }
//                print("ok")
        
        }
        
    }
    
    
    //设置UI
    private func setupUI(){
    
        addSubview(backImageView)
        
//        let toolbar = UIToolbar(frame: backImageView.bounds)
//        
//        addSubview(toolbar)
        
        
            addSubview(sloganImageView)
        
        
        //设置背景login图片的约束
        sloganImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(self.snp_top).offset(100)

        }
        

        addChildButtons()

    }
    
    //点击按钮的事件
    @objc private func btnDidClick(btn:LYComposeButton){
    
        //效果---被点击的图标方法--不被点击的缩小
        for button in buttons{
        
            var scale:CGFloat = 0
            if button == btn {
            
                scale = 1.5
                
            }else{
            
                scale = 0.4
            }
     
        
        UIView.animateWithDuration(0.25, animations: { 
        
            button.alpha = 0.25
            button.transform = CGAffineTransformMakeScale(scale, scale)
            
            }) { (_) in
         
//                print("ok")
        
            }
       
        }
       
        //MARK:点击按钮跳转页面
        print(btn.composeItem)
        guard let target = btn.composeItem?.target else{
          
            return
        }
        
        let type = NSClassFromString(target) as! UIViewController.Type
        
        let vc = type.init()
        
        
        let nav = UINavigationController(rootViewController: vc)
        
        self.target?.presentViewController(nav, animated: true, completion: {
            
            //跳转完成后,清楚
            self.removeFromSuperview()
        })
        
            
    }
    
   
    
    //添加按钮
    private func addChildButtons(){
    
        let count = composeItems.count
        
        let width:CGFloat = 80
        let height:CGFloat = 120
        //间距
        let magin:CGFloat = (ScreeWidth - 3*width)/4
        
        for i in 0..<count {
        
            let btn = LYComposeButton()
            
            
            let item = composeItems[i]

            //给按钮赋值模型
            btn.composeItem = item
            
         
            //布局设置frame
            //行
            let row = i/3
            //列
            let col = i%3
            
            let x = (magin+width) * CGFloat(col) + magin

            //设置按钮的y 一开始
            let y = (magin+height) * CGFloat(row) + ScreeHeight
            
            btn.frame = CGRect(x: x, y: y, width: width, height: height)
            
            //添加
            self.addSubview(btn)
            
            btn.addTarget(self, action:#selector(LYComposeView.btnDidClick(_:)) , forControlEvents: .TouchUpInside)
            
            //讲按钮添加到数组中
            buttons.append(btn)
       
        }
        
        
    }
    
    //懒加载控件  实现
    private lazy var backImageView:UIImageView = UIImageView(image: UIImage.snapShotCurrentWindow().applyLightEffect())
    
    private lazy var sloganImageView:UIImageView = UIImageView(image: UIImage(named: "compose_slogan"))
    
    
    
    //数组
    private lazy var composeItems:[LYComposeItems] = {
    
        var array = [LYComposeItems]()
        
        //获取文件路径
        let path = NSBundle.mainBundle().pathForResource("Compose.plist", ofType: nil)!
        
        //读取路径下的数据
        let itemArray = NSArray(contentsOfFile: path) as! [[String:AnyObject]]
        
        //遍历数组
        for item in itemArray{
        //字典转模型
            let composeItem = LYComposeItems(dict: item)
            array.append(composeItem)
        }
        
        return array
        
        
    }()
    
}
